import pandas as pd
import requests
import numpy as np
import time
from json_util import  *

from datetime import datetime


def send_req(code):
    """
    当日行情 分时 数据
    :param code: 股票6位代码
    :return:
    """
    if code[0] == '6':# 上海 股票
        url = "https://web.ifzq.gtimg.cn/appstock/app/minute/query?code=sh{}".format(code)
    else: # 深圳股票  创业板
        url = "https://web.ifzq.gtimg.cn/appstock/app/minute/query?code=sz{}".format(code)
    res = requests.get(url)# get 请求
    return res.json()

def batch_download_stock(codes):
    """
     批量下载全部股票

    :param codes:
    :return:
    """
    i =0
    res = {}
    # 假设codes 是 dict 的keys 或者 list
    for  code  in codes:
        # 获取某code 金融产品的数据， 保存到dict
        res[code] = send_req(code)
        i +=1
        if i%100 == 0:
            print(i)# 显示进度
            #break
    # 返回所有codes 的数据
    return res

code2name = load_json('stock_code2name.json')
res = batch_download_stock(code2name.keys())
file_name = '{}all_stock.json'.format(str(datetime.today()).split()[0])
save_json(file_name,res)

